

import { defHttp } from '/@/utils/http/axios';
import { LoginParams, LoginResultModel, GetUserInfoModel } from './model/userModel';
import {encryptByBase64} from '/@/utils/cipher.ts';
import { ErrorMessageMode } from '/#/axios';
import { useMessage } from '/@/hooks/web/useMessage';
import { TOKEN_KEY } from '/@/enums/cacheEnum';
import { router } from '/@/router';
import { PageEnum } from '/@/enums/pageEnum';
import { getIntoPageTenantId } from '/@/utils/tenant/index';
import { useGlobSetting } from '/@/hooks/setting';
import { setCacheByDynKey, getCacheByDynKey } from '/@/utils/cache/index';
const glob = useGlobSetting();
const { createErrorModal } = useMessage();
enum Api {
  // 获取第三方oauth2授权地址
  thirdAuthRender = '/auth-service/thirdAuth/authorize',
  // 三方登录
  thirdAuthLogin = '/auth-service/thirdAuth/login',
  // 首次进行绑定
  firstBind = '/auth-service/thirdAuth/bind',
  //获取二维码信息
  getLoginQrcode = '/auth-service/thirdAuth/getLoginQrcode',
  //监控二维码扫描状态
  getQrcodeToken = '/auth-service/thirdAuth/getQrcodeToken',
  //新加的获取图形验证码的接口
  getImageCaptcha = '/auth-service/captcha/get',
  checkImageCaptcha = '/auth-service/captcha/check',
}

export const getImageCaptcha = (captchaType = 'image') => defHttp.post({ url: Api.getImageCaptcha, params: { captchaType } })
export const checkImageCaptcha = (params) => {
  return defHttp.post(
    { url: Api.checkImageCaptcha, params }, 
    { isTransformResponse: false, errorMessageMode: 'none' }
  )
}

/**
 * 获取第三方授权地址
 */
export const thirdAuthRender = ({ grantType, backUrl }) => {
  setCacheByDynKey('grantType', grantType)
  const tenantId = getIntoPageTenantId()
  return defHttp.get({ url: `${Api.thirdAuthRender}/${grantType}`, params: {   
    backUrl,
    tenantId,
    client: glob.oauth2ClientId
  }});
};

export const thirdAuthLogin = (params) => {
  const grantType = getCacheByDynKey('grantType')
  const tenantId = getIntoPageTenantId()
  return defHttp.get({ url: `${Api.thirdAuthLogin}/${grantType}`, params: {
    ...params, 
    tenantId,
    client: glob.oauth2ClientId
  } });
};

export const thirdUserFirstBind = (params) => {
  return defHttp.post({ url: `${Api.firstBind}`, params }, { isTransformResponse: false });
};


/**
 * 获取登录二维码信息
 */
export function getLoginQrcode({ grantType }) {
  let url = Api.getLoginQrcode;
  return defHttp.get({ url: url, params: { clientId: glob.oauth2ClientId, grantType} });
}

/**
 * 监控扫码状态
 */
export function getQrcodeToken(params) {
  let url = Api.getQrcodeToken;
  return defHttp.get({ url: url, params }, { isTransformResponse: false });
}

